package hello.controller;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述：测试302跳转<br>
 * 版权：Copyright (c) 2011 ~ 2017<br>
 * 公司：北京活力天汇<br>
 * 作者：刘德咏<br>
 * 版本：1.0<br>
 * 创建日期：2017/9/6<br>
 */
@Controller
public class Test302Servlet {
    private Logger logger = LoggerFactory.getLogger(Test302Servlet.class);
    @RequestMapping("/hello1")
    public void hello(HttpServletResponse response) {
        try {
            response.sendRedirect("https://intlmapi.alipay.com/gateway.do?_input_charset=UTF-8&currency=CNY&ext_params=%7b%22VCN_ORG%22%3a%22UATP%22%7d&goods_description=United--Reservation&goods_title=United--RES&partner=2088411958404103&partner_trans_id=D7789379-93E3-426E-A542-B06E7053E598&pricing_currency=CNY&return_url=https%3a%2f%2fwww.united.com%2fual%2fzh-cn%2fcn%2fflight-search%2fbook-a-flight%2fbillinginfo%2falipaysuccess&seller_id=2088411958404103&service=alipay.overseas.forex.order.pay&service_type=100&sign_type=RSA&total_fee=2653.00&sign=UDBA30ep4Y2yID%2bwDqB2beGQiTZsDjUV%2fcvog7gDZC7IJ4fjKc1m08Qkkk0JLoJJOfRK6cy7s%2bCi6xwL3WYU5zpjuuSmDoNe%2bFnpzMT8eDOL4OTGqNmAtRMaxq7kY1fweRcJAlLNkSVCmqDlJHCfd3os%2br51NxgGp8bHhKMH0kQ%3d");
            // response.sendRedirect("http://192.168.3.116:8080/hello2");
            logger.debug(response.getHeader("location"));
        } catch (IOException e) {
            logger.error("{}", e);
            e.printStackTrace();
        }
    }

    @RequestMapping("/hello2")
    public void hello2(HttpServletResponse response) {
        try {
            response.sendRedirect("http://192.168.3.116:8080/hello3");
            logger.debug(response.getHeader("location"));
        } catch (IOException e) {
            logger.error("{}", e);
            e.printStackTrace();
        }
    }

    @RequestMapping("/hello3")
    @ResponseBody
    public String hello3() {
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//        try {
//            // 创建httpget.
//            HttpGet httpget = new HttpGet("http://192.168.3.116:8080/hello4");
//            System.out.println("executing request " + httpget.getURI());
//            // 执行get请求.
//            CloseableHttpResponse response = httpclient.execute(httpget);
//            logger.info(response.getEntity().toString());
//        } catch (ClientProtocolException e) {
//            e.printStackTrace();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            // 关闭连接,释放资源
//            try {
//                httpclient.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        return "hello3;";
    }
    
    @RequestMapping("/hello4")
    @ResponseBody
    public String hello4() {
       return "测试完成new1222";
    }

    @RequestMapping("/hello5")
    @ResponseBody
    public String hello5() {
        return "新测试";
    }

    @RequestMapping("/hello")
    public void hello() {
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet("http://192.168.3.116:8080/hello1");
            httpget.setConfig(config);
            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            int code = response.getStatusLine().getStatusCode();
            logger.debug("code:{}", code);
            if (code == 302) {
                logger.info("{}", response.getFirstHeader("location"));
                code = response.getStatusLine().getStatusCode();
                System.out.println("login"+code);
            }
            try {
                // 打印第一个302链接
                logger.debug("response:{}", response);
                logger.debug("Location:{}", response.getHeaders("Location"));
                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
